package com.lottery.ruok.lottrey.chat.model.extension;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;

import com.android.lib.dialog.BottomDialog;
import com.android.lib.dialog.Builder;
import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.GiveRewardDetailActivity;
import com.lottery.ruok.lottrey.activity.LoginActivity;
import com.lottery.ruok.lottrey.activity.NewChannelInfoActivity;
import com.lottery.ruok.lottrey.activity.SearchGivePrizeActivity;
import com.lottery.ruok.lottrey.activity.SendRedPacketActivity;
import com.lottery.ruok.lottrey.activity.SingleSendRedPacketActivity;
import com.lottery.ruok.lottrey.common.Common;
import com.lottery.ruok.lottrey.userInfo.UserInfo;

import io.rong.imkit.RongExtension;
import io.rong.imkit.plugin.IPluginModule;
import io.rong.imlib.model.Conversation;
import utils.Utils;

import static com.lottery.ruok.lottrey.activity.SendRedPacketActivity.EXTRA_TARGET_ID;
import static com.lottery.ruok.lottrey.activity.SendRedPacketActivity.EXTRA_TYPE;

/**
 * Created by Beyond on 2016/11/14.
 */

public class RedPacketPlugin implements IPluginModule {

    private Context context;
    private Conversation.ConversationType conversationType;
    private String targetId;

    public RedPacketPlugin() {
    }

    @Override
    public Drawable obtainDrawable(Context context) {
        return ContextCompat.getDrawable(context, R.drawable.icon_redpacket);
    }

    @Override
    public String obtainTitle(Context context) {
        return context.getString(R.string.red_packet_name);
    }

    /**
     * 点击扩展下面的icon
     *
     * @param currentFragment
     * @param extension
     */
    @Override
    public void onClick(Fragment currentFragment, final RongExtension extension) {
        if (TextUtils.isEmpty(UserInfo.getToken())) {
            //Utils.showToast("未登录,不能发红包");
            FragmentActivity activity = currentFragment.getActivity();
            activity.startActivity(new Intent(activity, LoginActivity.class));
            activity.overridePendingTransition(R.anim.anim_enter_from_bottom, R.anim.activity_stay);
        } else if (UserInfo.getIsTest() == 1 && UserInfo.getIsTry() != 1) {
            Utils.showToast(Common.IS_TEST);
        } else {
            int type = 0;
            context = currentFragment.getActivity();
            conversationType = extension.getConversationType();
            targetId = extension.getTargetId();
            if (extension.getConversationType().getName().equals("chatroom")) {
                type = SendRedPacketActivity.TYPE_GROUP_NORMAL;

                final Builder builder = new Builder(context);
                final int finalType = type;
                builder.addOption("红包", Color.parseColor("#007aff"), new BottomDialog.OnOptionClickListener() {
                    @Override
                    public void onOptionClick() {
                        Intent intent = new Intent(context, SendRedPacketActivity.class);
                        intent.putExtra(EXTRA_TYPE, finalType);
                        intent.putExtra(EXTRA_TARGET_ID, targetId);
                        context.startActivity(intent);
                    }
                });
                builder.addOption("打赏", Color.parseColor("#007aff"), new BottomDialog.OnOptionClickListener() {
                    @Override
                    public void onOptionClick() {
                        SearchGivePrizeActivity.startActivity(context, targetId, conversationType);
                    }
                });
                builder.create().show();


            } else if (extension.getConversationType().getName().equals("private")) {
                type = SendRedPacketActivity.TYPE_PRIVITE;
                Intent intent = new Intent(context, SingleSendRedPacketActivity.class);
                intent.putExtra(EXTRA_TYPE, type);
                intent.putExtra(EXTRA_TARGET_ID, targetId);
                context.startActivity(intent);
            }


            //  SendRedPacketActivity.startActivity(context, targetId, conversationType);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
//        if (requestCode == REQUEST_CONTACT && resultCode == Activity.RESULT_OK) {
//            Intent intent = new Intent(context, ContactDetailActivity.class);
//            intent.putExtra("contact", data.getParcelableExtra("contact"));
//            intent.putExtra("conversationType", conversationType);
//            intent.putExtra("targetId", targetId);
//            context.startActivity(intent);
//        }
    }
}
